// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#include "MikuMikuDance.h"

#include "ISettingsModule.h"
#include "ISettingsSection.h"
#include "ISettingsContainer.h"

#include "MikuMikuDanceSettings.h"

#define LOCTEXT_NAMESPACE "FMikuMikuDanceModule"

void FMikuMikuDanceModule::StartupModule()
{
	RegisterSettings();
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
}

void FMikuMikuDanceModule::ShutdownModule()
{
	UnregisterSettings();
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.
}

bool FMikuMikuDanceModule::SupportsDynamicReloading()
{
	return true;
}

bool FMikuMikuDanceModule::HandleSettingsSaved()
{
	UMikuMikuDanceSettings* Setting = GetMutableDefault<UMikuMikuDanceSettings>();
	bool ResaveSetting = false;
	if (ResaveSetting) {
		Setting->SaveConfig();
	}
	return true;
}

void FMikuMikuDanceModule::RegisterSettings()
{
	if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings")) {
		ISettingsContainerPtr SettingsContainer = SettingsModule->GetContainer("Project");
		SettingsContainer->DescribeCategory("CustomSettings", LOCTEXT("RuntimeWDCategoryName", "CustomSettings"), LOCTEXT("RuntimeWDCategoryDescription", "a"));

		ISettingsSectionPtr SettingsSection = SettingsModule->RegisterSettings("Project", "Plugins", "Miku Miku Dance", LOCTEXT("RuntimeGeneralSettingName", "Miku Miku Dance"), LOCTEXT("RuntimeGeneralSettingDescription", "Miku Miku Dance Setting"), GetMutableDefault<UMikuMikuDanceSettings>());
		if (SettingsSection.IsValid()) {
			SettingsSection->OnModified().BindRaw(this, &FMikuMikuDanceModule::HandleSettingsSaved);
		}
	}
}

void FMikuMikuDanceModule::UnregisterSettings()
{
	if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings")) {
		SettingsModule->UnregisterSettings("Project", "Plugins", "Miku Miku Dance");
	}
}

#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FMikuMikuDanceModule, MikuMikuDance)